//----------- Data Merging & Cleaning ---------------//

drop _all
clear matrix
global DIR "C:\Replication package"
cd "${DIR}"

//--- JK2019 ECB surprises ---//

import excel "${DIR}\Input data\JK2019_ecbshocks_daily.xlsx", sheet("ecbshocks_d") cellrange(A1:K291) firstrow clear
rename *, lower
gen date = mdy(month,day,year)
format %td date
tsset date
drop month day t1 t2 
rename stoxx50_jk_njt stoxx50_hf
rename pc1eon1_jk_njt pc1eon1_hf
rename mppoormand_pc1eon1stoxx50 jk19_mppoor
rename cbipoormand_pc1eon1stoxx50 jk19_cbipoor
rename mpsignd_pc1eon1stoxx50 jk19_mpsign
rename cbisignd_pc1eon1stoxx50 jk19_cbisign
gen mdate = mofd(date)
format %tm mdate
collapse (first) year (sum) stoxx50_hf-jk19_cbisign, by(mdate)
foreach var of varlist stoxx50_hf-jk19_cbisign {
label variable `var'
}
tsset mdate
save "${DIR}\JK19_shocks.dta", replace

********************************************
***   FIGURE 1 - GRAPH Daily MP shocks   ***
********************************************

twoway (connected jk19_mpsign mdate, lcolor(black) lwidth(medium) mcolor(black) msymbol(circle) msize(medlarge) mfcolor(none)) ///
if year>=2007 & year<=2019, xtitle("") xlabel(564(24)710, format(%tmCCYY) labsize(medsmall)) ///
 title("Monthly MP shocks" "(in basis points)", size(medium)) ///
ylabel(-0.15(0.05)0.15, angle(horiz) axis(1)) yscale(titlegap(*-35)) ytitle("") yline(0)  ///
graphregion(fcolor(white) lcolor(white)) plotregion(fcolor(white) lcolor(black)) ///
legend(off) aspectratio(0.9) scale(*1.2) xsize(4.5) ysize(4)
graph export "Fig1_MonthlyMP.png", width(1800) height(1600) replace
graph export "Fig1_MonthlyMP.eps", replace

//--- Kerssenfischer2019 ECB surprises ---//

import excel "${DIR}\Input data\Kerssenfischer2019_shocks.xlsx", sheet("shocks") cellrange(A1:D211) firstrow clear
rename *, lower
rename time date
format %td date
tsset date
rename purepolicy k19_mp
gen mdate = mofd(date)
format %tm mdate
collapse (sum) k19_mp, by(mdate)
label variable k19_mp
tsset mdate
save "${DIR}\K19_shocks.dta", replace

********************************************
***   FIGURE 1 - GRAPH YEARLY MP shocks  ***
********************************************

import excel "${DIR}\Input data\JK2019_ecbshocks_daily.xlsx", sheet("ecbshocks_d") cellrange(A1:K291) firstrow clear
rename *, lower
gen date = mdy(month,day,year)
format %td date
tsset date
rename mpsignd_pc1eon1stoxx50 jk19_mpsign
gen mdate = mofd(date)
format %tm mdate
collapse (sum) jk19_mpsign, by(year)
label variable jk19_mpsign
save "JK19_YEAR.dta", replace
import excel "${DIR}\Input data\Kerssenfischer2019_shocks.xlsx", sheet("shocks") cellrange(A1:D211) firstrow clear
rename *, lower
rename time date
format %td date
tsset date
rename purepolicy k19_mp
gen mdate = mofd(date)
format %tm mdate
gen year = yofd(date)
collapse (sum) k19_mp, by(year)
label variable k19_mp
merge 1:1 year using "JK19_YEAR.dta"
drop if _merge==1
drop if _merge==2
drop _merge
tsset year
cap drop mp_shock1 mp_shock2
egen mp_shock1 = std(jk19_mpsign) if year>=2007 & year<=2019
egen mp_shock2 = std(k19_mp) if year>=2007 & year<=2019
sum jk19_mpsign mp_shock1 mp_shock2 if year>=2007 & year<=2019 
twoway (connected mp_shock2 year, lcolor(gs11) mcolor(gs11) msymbol(diamond) msize(medium) mfcolor(none)) ///
(connected mp_shock1 year, lcolor(black) lwidth(medthick) mcolor(black) msymbol(circle) msize(medlarge) mfcolor(none)) ///
if year>=2007 & year<=2019, xtitle("") xlabel(2007(2)2019, labsize(medsmall)) title("Cumulated Yearly MP shocks" "(normalised to 1 SD)", size(medium)) ///
ylabel(-2(1)3, angle(horiz) axis(1)) yscale(titlegap(*-35)) ytitle("") yline(0)  ///
graphregion(fcolor(white) lcolor(white)) plotregion(fcolor(white) lcolor(black)) ///
legend(order(2 "JK20" 1 "K19") position(7) ring(0) col(1) size(small) keyg(*.5) rowg(*.2) colgap(*.2) symy(1) symx(5)) ///
aspectratio(0.9) scale(*1.2) xsize(4.5) ysize(4)
graph export "Fig1_YearlyMP.png", width(1800) height(1600) replace
graph export "Fig1_YearlyMP.eps", replace

//--- Daily series ---//

import excel "${DIR}\Input data\Datastream_Daily_series.xlsx", sheet("Stata") cellrange(A1:M5805) firstrow clear
rename *, lower
format %td date
tsset date
rename eueonia eonia
rename oieur* ois*
rename oissw ois1w
rename oieu10y ois10y
gen mdate = mofd(date)
format %tm mdate
collapse (last) eonia-ois10y, by(mdate)
foreach var of varlist eonia-ois10y {
label variable `var'
}
tsset mdate
save "${DIR}\Policy_series.dta", replace

//--- Unemp & labor costs 1 ---//

import excel "${DIR}\Input data\Datastream_labour_costs_unemployment.xlsx", sheet("unemployment") cellrange(A1:C301) firstrow clear
rename *, lower
rename a date
rename frunemploymentratestandardiz unemp 
rename frharmonisedunemplmtrateal unemp2
format %td date
gen mdate = mofd(date)
format %tm mdate
drop date
tsset mdate
save "${DIR}\Unemp.dta", replace

//--- Unemp & labor costs 2 ---//

import excel "${DIR}\Input data\Datastream_labour_costs_unemployment.xlsx", sheet("labour costs quarterly") cellrange(A1:C102) firstrow clear
rename *, lower
drop a
rename frearningsperempquarterly earnings_q
format %td date
gen mdate = mofd(date)
format %tm mdate
tsset mdate
tsfill
ipolate earnings_q mdate, gen(earnings)
drop date earnings_q
save "${DIR}\ULCquarterly.dta", replace


//--- MERGE MONTHLY DATA ---//

use "JK19_shocks.dta", clear

merge 1:1 mdate using "K19_shocks.dta"
drop _merge

merge 1:1 mdate using "Policy_series.dta"
drop _merge

merge 1:1 mdate using "Unemp.dta"
drop _merge

merge 1:1 mdate using "ULCquarterly.dta"
drop _merge

save "Monthly.dta", replace

********************************************************
***   FIGURE A1 - MP effects on Unemp and Earnings   ***
********************************************************

use "Monthly.dta", clear
tsset mdate

gen xaxis = 0 if _n==1
replace xaxis = xaxis[_n-1]+1 if _n>1
replace xaxis = . if _n>25
gen zero = 0

sum jk19_mpsign
egen jk19_mpsign_std = std(jk19_mpsign)
replace jk19_mpsign_std = -1*jk19_mpsign_std

gen earnings_g = (earnings - L12.earnings)/L12.earnings*100

//---- Earnings
local variable earnings_g
local forward F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12
foreach var of local variable {
cap drop irf1_*
foreach lead of local forward {
quiet reg `lead'.`var' jk19_mpsign_std L(1/2).earnings_g if mdate>=tm(2007m1) & mdate<=tm(2019m12), vce(rob)
quiet global mps_d2y jk19_mpsign_std
quiet lincom $mps_d2y
scalar p_c_`lead'_`var'=r(estimate)
scalar p_se_`lead'_`var'=r(se)
scalar p_a1_`lead'_`var' = p_c_`lead'_`var' + 1*p_se_`lead'_`var'
scalar p_b1_`lead'_`var' = p_c_`lead'_`var' - 1*p_se_`lead'_`var'
scalar p_a2_`lead'_`var' = p_c_`lead'_`var' + 1.64*p_se_`lead'_`var'
scalar p_b2_`lead'_`var' = p_c_`lead'_`var' - 1.64*p_se_`lead'_`var'
matrix alpha11=nullmat(alpha11), p_a2_`lead'_`var'
matrix alpha12=nullmat(alpha12), p_a1_`lead'_`var'
matrix alpha13=nullmat(alpha13), p_c_`lead'_`var'
matrix alpha14=nullmat(alpha14), p_b1_`lead'_`var'
matrix alpha15=nullmat(alpha15), p_b2_`lead'_`var'
matrix alpha16=alpha11\alpha12\alpha13\alpha14\alpha15
}
matrix alpha16=alpha16'
svmat alpha16, names(irf1_`var'_)
scalar drop _all
mat drop _all
//---- Graph
twoway (rarea irf1_`var'_1 irf1_`var'_5 xaxis, fcolor(stone) fintensity(15) lcolor(stone) lwidth(none)) ///
(rarea irf1_`var'_2 irf1_`var'_4 xaxis, fcolor(stone) fintensity(75) lcolor(stone) lwidth(none)) ///
(connected irf1_`var'_3 xaxis, lcolor(black) mcolor(black) msymbol(circle) mfcolor(none)) ///
(line zero xaxis, lcolor(red) lwidth(thin)) if _n<14, ///
title("", size(medium) color(black)) xtitle("") name(`var', replace) ///
ylabel(-0.1(0.05)0.15, angle(horizontal) axis(1)) ytitle("", axis(1)) ///
xlabel(0(3)12, labsize(medium)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
plotregion(fcolor(white) lcolor(black) ifcolor(white) ilcolor(white)) ///
legend(cols(1) size(medsmall) order(3 "Earnings") rowgap(*.01) colgap(*.01) ring(0) position(11) ///
symysize(3) symxsize(5)) aspectratio(0.9) scale(*1.2) xsize(4.5) ysize(4)
}
graph export "FigA1_MonthlyIRF_Earnings.png", width(1800) height(1600) replace
graph export "FigA1_MonthlyIRF_Earnings.eps", replace

//---- Unemp
local variable unemp
local forward F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12
foreach var of local variable {
cap drop irf2_*
foreach lead of local forward {
quiet reg `lead'.`var' jk19_mpsign_std L(1/2).unemp if mdate>=tm(2007m1) & mdate<=tm(2019m12), vce(rob)
quiet global mps_d2y jk19_mpsign_std
quiet lincom $mps_d2y
scalar p_c_`lead'_`var'=r(estimate)
scalar p_se_`lead'_`var'=r(se)
scalar p_a1_`lead'_`var' = p_c_`lead'_`var' + 1*p_se_`lead'_`var'
scalar p_b1_`lead'_`var' = p_c_`lead'_`var' - 1*p_se_`lead'_`var'
scalar p_a2_`lead'_`var' = p_c_`lead'_`var' + 1.64*p_se_`lead'_`var'
scalar p_b2_`lead'_`var' = p_c_`lead'_`var' - 1.64*p_se_`lead'_`var'
matrix alpha11=nullmat(alpha11), p_a2_`lead'_`var'
matrix alpha12=nullmat(alpha12), p_a1_`lead'_`var'
matrix alpha13=nullmat(alpha13), p_c_`lead'_`var'
matrix alpha14=nullmat(alpha14), p_b1_`lead'_`var'
matrix alpha15=nullmat(alpha15), p_b2_`lead'_`var'
matrix alpha16=alpha11\alpha12\alpha13\alpha14\alpha15
}
matrix alpha16=alpha16'
svmat alpha16, names(irf2_`var'_)
scalar drop _all
mat drop _all
//---- Graph
twoway (rarea irf2_`var'_1 irf2_`var'_5 xaxis, fcolor(stone) fintensity(15) lcolor(stone) lwidth(none)) ///
(rarea irf2_`var'_2 irf2_`var'_4 xaxis, fcolor(stone) fintensity(75) lcolor(stone) lwidth(none)) ///
(connected irf2_`var'_3 xaxis, lcolor(black) mcolor(black) msymbol(circle) mfcolor(none)) ///
(line zero xaxis, lcolor(red) lwidth(thin)) if _n<14, ///
title("", size(medium) color(black)) xtitle("") name(`var', replace) ///
ylabel(-0.2(0.1)0.1, angle(horizontal) axis(1)) ytitle("", axis(1)) ///
xlabel(0(3)12, labsize(medium)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
plotregion(fcolor(white) lcolor(black) ifcolor(white) ilcolor(white)) ///
legend(cols(1) size(medsmall) order(3 "Unemployment") rowgap(*.01) colgap(*.01) ring(0) position(11) ///
symysize(3) symxsize(5)) aspectratio(0.9) scale(*1.2) xsize(4.5) ysize(4)
}
graph export "FigA1_MonthlyIRF_Unemp.png", width(1800) height(1600) replace
graph export "FigA1_MonthlyIRF_Unemp.eps", replace

sum irf1_earnings_g_3 irf2_unemp_3

cap drop beta_earnings beta_unemp
egen beta_earnings = total(irf1_earnings_g_3)
egen beta_unemp = total(irf2_unemp_3)
sum beta*


*** COLLAPSE TO YEARLY FREQUENCY ***

gen date = year(dofm(mdate))
gen month = month(dofm(mdate))

keep mdate date month jk19_mppoor jk19_mpsign k19_mp

order mdate date month jk19_mppoor jk19_mpsign k19_mp

gen jk19_mpsign_w = jk19_mpsign*(12-month+1)/12

collapse (sum) jk19_mppoor-jk19_mpsign_w, by(date)

foreach var of varlist jk19_mppoor-jk19_mpsign_w {
label variable `var'
}

format %ty date
tsset date

tsline jk19_mpsign jk19_mpsign_w

save "Monthly.dta", replace


//--- Quarterly Macro series ---//

*** IPCH FRANCE MENSUEL (SOURCE INSEE)
import excel using "${DIR}\Input data\IPCH_MENS_FIN.xlsx", firstrow sheet("fin") clear
gen annee=year(madate)
gen mois=month(madate)
gen trim= 9
replace trim =1 if (mois == 1 | mois == 2 | mois == 3)
replace trim =2 if (mois == 4 | mois == 5 | mois == 6)
replace trim =3 if (mois == 7 | mois == 8 | mois == 9)
replace trim =4 if (mois == 10 | mois == 11 | mois == 12)
drop if annee < 1999
gen DATE = yq(annee,trim)
format DATE %tq
drop annee mois trim
order DATE 
sort DATE
collapse (mean) IPCH, by (DATE)
order DATE IPCH
save "${DIR}\IPCH_fin.dta", replace

*** DEFLATEUR DU PIB (SOURCE DATASTREAM)
import excel using "${DIR}\Input data\DEFL_PIB_FIN.xlsx", firstrow sheet("fin") clear
gen DATE = quarterly(madate, "YQ")
format DATE %tq
drop madate
drop if year(dofq(DATE)) < 1999
order DATE DEFL_PIB
save "${DIR}\DEFL_PIB_fin.dta", replace

*** MATCHING
use "${DIR}\IPCH_fin.dta", clear
merge m:m DATE using "${DIR}\DEFL_PIB_fin.dta"
drop _merge
rename DATE date
rename IPCH ipch
rename DEFL_PIB defpib
tsset date
gen year = year(dofq(date))
collapse (mean) ipch defpib, by(year)
foreach var of varlist ipch-defpib {
label variable `var'
}
rename year date 
format %ty date
tsset date
gen ipch_2018 = 1 if date==2018
replace ipch_2018 = ipch/ipch[20]
gen defpib_2018 = 1 if date==2018
replace defpib_2018 = defpib/defpib[20]
save "${DIR}\PriceLevel.dta", replace
cap erase "${DIR}\IPCH_fin.dta"
cap erase "${DIR}\DEFL_PIB_fin.dta"

*** EA MACRO
import excel "${DIR}\Input data\Datastream_EA_Macro.xlsx", sheet("EA") cellrange(A1:Y85) firstrow clear
rename *, lower
rename ea_2y ir_2y
rename ea_10y ir_10y
rename ea_* *
gen dateq = tq(1999q1) + _n-1
format %tq dateq
tsset dateq
drop wuxia krip qgrowth defgdp exp imp
gen year = year(dofq(dateq))
collapse (mean) rate-exch, by(year)
foreach var of varlist rate-exch {
label variable `var'
}
rename year date 
format %ty date
tsset date
save "${DIR}\Quarterly.dta", replace

*** MERGE QUARTERLY DATA

use "Quarterly.dta", clear
merge 1:1 date using "PriceLevel.dta"
drop if _merge==2
drop _merge
save "${DIR}\Quarterly.dta", replace


//--- MERGE ALL ---//

use "Monthly.dta", clear

merge 1:1 date using "Quarterly.dta"
drop _merge
drop if date>=2020

save "${DIR}\Input data\MPS-MacroData.dta", replace


*** Clean directory
cap erase "JK19_shocks.dta"
cap erase "K19_shocks.dta"
cap erase "K19_shocks2.dta"
cap erase "JK19_YEAR.dta"
cap erase "Policy_series.dta"
cap erase "Unemp.dta"
cap erase "ULCmonthly.dta"
cap erase "ULCquarterly.dta"
cap erase "Monthly.dta"
cap erase "PriceLevel.dta"
cap erase "Quarterly.dta"
